package jsyf.oa.web.ReportExcel;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;

import jsyf.com.util.BaseUtils;
import jsyf.com.util.ExcelUtil;
import jsyf.com.util.ExportExcel;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.entity.yuce.YuceMainEntity;
import jsyf.oa.facade.yuce.YuceMainFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Created by Maqiuqiu on 2018/8/9.
 */
/**
 *功能描述：导出报表
 */
@Api(value = "导出", description = "导出")
@Controller
@RequestMapping("/reportExcel")
public class ReportExcel {

    @Resource
    private YuceMainFacade yuceMainFacade;

    @RequestMapping(value = "/downExcel.json", method = RequestMethod.POST)
    public void downExcel(HttpServletResponse response, @RequestBody String jsonData) throws IOException {
//        String fileName = "8月销售预测";
//        JSONObject result = yuceMainFacade.select(jsonData);
//        List<YuceMainEntity> list = new ArrayList<>();
//        String columnNames[] = {
//
//        };// 列名
//        String keys[] = {
//
//        };// map中的key
//        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        try {
//            ExcelUtil.createWorkBook(result, keys, columnNames).write(os);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        byte[] content = os.toByteArray();
//        InputStream is = new ByteArrayInputStream(content);
//        // 设置response参数，可以打开下载页面
//        response.reset();
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");
//        response.setHeader("Content-Disposition",
//                "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
//        ServletOutputStream out = response.getOutputStream();
//        BufferedInputStream bis = null;
//        BufferedOutputStream bos = null;
//        try {
//            bis = new BufferedInputStream(is);
//            bos = new BufferedOutputStream(out);
//            byte[] buff = new byte[2048];
//            int bytesRead;
//            // Simple read/write loop.
//            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
//                bos.write(buff, 0, bytesRead);
//            }
//        } catch (final IOException e) {
//            throw e;
//        } finally {
//            if (bis != null)
//                bis.close();
//            if (bos != null)
//                bos.close();
//        }
//        return null;
    }

    public void export(String title, String[] rowName, List<Object[]> dataList,HttpServletResponse  response) throws Exception {
        ExportExcel exportExcel = new ExportExcel(title,rowName,dataList,response);
        exportExcel.export();
    }

}
